import { ref } from "vue";
import { defineStore } from "pinia";
// import { parse, stringify } from 'zipson'
export interface IUser {
  id: number | string;
  username: string;
  token?: string;
  gender?: string;
  isAdmin?: boolean;
  phone?: string;
  createTime?: string;
  updateTime?: string;
  age?: number;
  remark?: string;
}
const useUserStore = defineStore(
  "user",
  () => {
    const token = ref("");
    const user = ref<IUser>({
      id: "",
      username: "",
    });
    const projectName = ref(import.meta.env.VITE_NAME)

    const setToken = (t: string) => {
      token.value = t;
    };
    const setUser = (params: IUser) => {
      user.value = params;
    };
    return {
      projectName,
      token,
      setToken,
      user,
      setUser,
    };
  },
  {
    // persist: true, // 开启状态缓存(默认配置)
    persist: {
      key: "userStore", // 默认使用store.$id(user) storage缓存的key
      storage: localStorage, // 使用什么会话缓存localStorage('userStore', value)
      // [] 表示不持久化任何状态; undefined(默认) 或 null 表示持久化整个 state
      // paths: undefined,
      // 使用何种序列化方式
      // serializer: {
      //   deserialize: parse,
      //   serialize: stringify,
      // },
      beforeRestore: (ctx) => {
        // console.log(`即将恢复 '${ctx.store.$id}'`)
      },
      afterRestore: (ctx) => {
        // 引用类型经过序列化后无法再关联，可以再这里重新关联上
        // console.log(ctx.store);
        // console.log(`刚刚恢复完 '${ctx.store.$id}'`)
      },
    },
  }
);

export default useUserStore;
